﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AteneoLibri.BackEnd
{
    public partial class ManageBooks : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Reuperare i libri
            if (!Page.IsPostBack)
            {                
                List<AteneoLibriBusinessLogic.Entities.Book> books = new List<AteneoLibriBusinessLogic.Entities.Book>();
                using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                {
                    // NON SONO RIMOSSI ANCHE I LIBRI CHE IN REALTà SONO STATI CANCELLATI LOGICAMENTE
                    books = service.getBooks();
                }

                lblNumberOfBooks.Text = books.Count.ToString();

                Session["BACK_END_BOOK_LIST"] = books;
                LoadBooks();
            }
        }

        private void LoadBooks()
        {
            List<AteneoLibriBusinessLogic.Entities.Book> books = Session["BACK_END_BOOK_LIST"] as List<AteneoLibriBusinessLogic.Entities.Book>;

            gvBooks.DataSource = books.OrderByDescending(x => x.InsertDate).ThenBy(x => x.Title).ToList();
            gvBooks.DataBind();
        }

        protected void gvBooks_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBooks.PageIndex = e.NewPageIndex;
            LoadBooks();
        }
    }
}